package com.example.app1.globule;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.DisplayMetrics;
import android.view.View;

import java.util.ArrayList;
import java.util.Random;

public class TheScreenView extends View {
    //界面主线程的控制变量  
    private boolean drawing = false;  
    //储存当前已有的球的信息  
    private ArrayList<Circle> circles;  
    private Paint mPaint;  
    //球的运动范围
    public int WIDTH;
    public int HEIGHT;  
    public static final double PI = 3.14159265;  
    Paint mPaint2 = new Paint();  
    public TheScreenView(Context context) {  
        super(context);  
        circles = new ArrayList<Circle>();  
        //加入了两个球  
        circles.add(new Circle(50));
        circles.add(new Circle(40));
        circles.add(new Circle(77));
        circles.add(new Circle(25));
        circles.add(new Circle(30));

        mPaint = new Paint();
        mPaint.setColor(Color.YELLOW);
        mPaint.setAntiAlias(true);

        mPaint2.setStyle(Style.FILL);  
        mPaint2.setColor(Color.RED);  
        mPaint2.setAntiAlias(true);


        DisplayMetrics dm = new DisplayMetrics();
        ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(dm);
        WIDTH  = dm.widthPixels;
        HEIGHT = dm.heightPixels;
        
        //启动界面线程,开始自动更新界面  
        drawing = true;  
        new Thread(mRunnable).start();  
    }      
    private Runnable mRunnable = new Runnable() {  
        //界面的主线程  
        @Override  
        public void run() {  
            while( drawing )  
            {  
                try {  
                    //更新球的位置信息  
                    update();  
                    //通知系统更新界面,相当于调用了onDraw函数  
                    postInvalidate();  
                    //界面更新的频率,这里是每30ms更新一次界面  
                    Thread.sleep(30);  
                    //Log.e(TAG, "drawing");  
                } catch (InterruptedException e) {  
                    e.printStackTrace();  
                }  
            }  
        }  
    };        
    public void stopDrawing()  
    {  
        drawing = false;  
    }            
    @Override
    public void onDraw(Canvas canvas)  
    {     	
        //在canvas上绘制边框  
        canvas.drawRect(0, 0, WIDTH, HEIGHT, mPaint2);  
        //在canvas上绘制球  
        for( Circle circle : circles) {  
            canvas.drawCircle(circle.x, circle.y, circle.radius, mPaint);  
        }  
    }  
    //界面的逻辑函数,主要检查球是否发生碰撞,以及更新球的位置  
    private void update(){  
        if( circles.size()>1) {  
            for( int i1=0; i1<circles.size()-1; i1++)  {  
                //当两个球发生碰撞,交换两个球的角度值  
                for( int i2=i1+1; i2<circles.size(); i2++)  
                    if( checkBumb(circles.get(i1),circles.get(i2))){  
                        circles.get(i1).changeDerection(circles.get(i2));  
                    }  
            }             
        }  
        //更新球的位置  
        for( Circle circle: circles)  
            circle.updateLocate();  
    }        
    private boolean checkBumb(Circle c1, Circle c2) {  
        return (c1.x-c2.x)*(c1.x-c2.x) + (c1.y-c2.y)*(c1.y-c2.y) <= (c1.radius+c2.radius)*(c1.radius+c2.radius);           
    }       
    /** 
     * 自定义的View的内部类,存储每一个球的信息  
     */  
    class Circle {  
        float x=150;  
        float y=170;  
        double angle= (new Random().nextFloat())*2*PI;;  
        int speed=20;  
        int radius=20;       
        public Circle() {  
        }       
        public Circle( float x, float y, int r ){  
            this.x = x;  
            this.y = y;  
            radius = r;  
        }

        public Circle(int radius) {
            this.radius = radius;
        }

        //利用三角函数计算出球的新位置值,当与边界发生碰撞时,改变球的角度  
        public void updateLocate()   {  
            x = x+ (float)(speed *Math.cos(angle));  
            //Log.v(TAG, Math.cos(angle)+"");  
            y = y+ (float)(speed *Math.sin(angle));  
            //Log.v(TAG, Math.cos(angle)+"");  
            if( (x+radius)>=WIDTH )    {  
                if( angle >=0 && angle <= (PI/2))  
                    angle = PI - angle;  
                if( angle > 1.5 * PI && angle <= 2*PI)  
                    angle = 3 * PI - angle;               
            }  
            if( x-radius <=0 )   {  
                if( angle >= PI && angle <= 1.5*PI )  
                    angle = 3*PI - angle;  
                if( angle >= PI/2 && angle < PI)  
                    angle = PI - angle;  
            }  
            if( y-radius<=0 || y+radius>=HEIGHT)  
                angle = 2*PI - angle;              
        }  
        //两球交换角度  
        public void changeDerection(Circle other)  {  
            double temp = this.angle;  
            this.angle = other.angle;  
            other.angle = temp;  
        }  
    }  
}  